package com.hunttown.mes.manage.service;

import com.hunttown.mes.rpc.domain.TransportExpenseDTO;
import com.hunttown.mes.rpc.domain.query.TransportExpenseDTOQuery;
import com.hunttown.common.domain.Page;
import com.hunttown.mes.common.utils.FormatUtils;
import com.hunttown.mes.rpc.api.TransportExpenseRpcService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * created by wangjunfu on 2019-07-07 10:50:27
 */
@Service
public class TransportExpenseManageService {

    private final static Logger logger = LoggerFactory.getLogger(TransportExpenseManageService.class);

    private final TransportExpenseRpcService rpcService;

    @Autowired
    public TransportExpenseManageService(TransportExpenseRpcService rpcService) {
        this.rpcService = rpcService;
    }

    // 新增
    public TransportExpenseDTO insert(TransportExpenseDTO objDTO) {
        return rpcService.insert(objDTO);
    }

    // 通过ID修改
    public Boolean updateInfoById(TransportExpenseDTO objDTO) {
        return rpcService.updateInfoById(objDTO);
    }

    // 通过Query修改
    public Boolean updateInfoByQuery(TransportExpenseDTOQuery objDTOQuery) {
        return rpcService.updateInfoByQuery(objDTOQuery);
    }

    // 通过ID删除
    public Boolean delete(Integer id) {
        return rpcService.deleteById(id);
    }

    // 批量删除
    public Boolean deleteForBatch(String clearIds) {

        //处理 “,5,6,7,” 为List
        List<Integer> idList = FormatUtils.commaToList_Integer(clearIds);

        if (idList.size() == 0) {
            return false;
        }

        TransportExpenseDTOQuery query = new TransportExpenseDTOQuery();
        query.setDeleteFlag(1);
        query.setIds(idList);

        return rpcService.updateInfoByQuery(query);
    }

    // 通过ID获取
    public TransportExpenseDTO getById(Integer id) {
        if (id <= 0) {
            return null;
        }
        return rpcService.selectById(id);
    }

    // 通过Query获取
    public TransportExpenseDTO getByQuery(TransportExpenseDTOQuery objDTOQuery) {
        return rpcService.selectObjByQuery(objDTOQuery);
    }

    // 获取分页列表
    public Page<TransportExpenseDTO> getForPage(TransportExpenseDTOQuery objDTOQuery) {
        return rpcService.getListForPage(objDTOQuery);
    }

    public Page<TransportExpenseDTO> getJoinSecondaryForPage(TransportExpenseDTOQuery objDTOQuery) {
        return rpcService.getJoinSecondaryForPage(objDTOQuery);
    }

    // 改变某条记录的使用状态
    public Boolean changeState(Integer id, Integer state) {
        TransportExpenseDTO obj = new TransportExpenseDTO();
        obj.setStopFlag(state);
        obj.setId(id);
        return updateInfoById(obj);
    }

    //递归查询拆单的子单，并存放在同一级目录下（方便查看和理解）
    public List<TransportExpenseDTO> getSplitOrderByRecursion(Integer parentId) {
        List<TransportExpenseDTO> returnList = new ArrayList<>();

        if (parentId == null || parentId <= 0) {
            return returnList;
        }

        TransportExpenseDTOQuery childQuery = new TransportExpenseDTOQuery();
        childQuery.setParentId(parentId);
        Page<TransportExpenseDTO> childPage = getForPage(childQuery);
        for (TransportExpenseDTO item : childPage.getItems()) {
            returnList.add(item);

            List<TransportExpenseDTO> next_list = getSplitOrderByRecursion(item.getId());
            returnList.addAll(next_list);
        }

        return returnList;
    }

    //统计-报销
    public List<Map<String, String>> statisticExpense(Integer searchYear) {
        return rpcService.statisticExpense(searchYear);
    }

    //统计-报销（各渠道）
    public List<Map<String, String>> statisticExpenseByType(Integer searchYear) {
        return rpcService.statisticExpenseByType(searchYear);
    }

    //统计-综合报销（各项目）
    public List<Map<String, String>> statisticZHExpense(Integer searchYear) {
        return rpcService.statisticZHExpense(searchYear);
    }
}